#!/usr/bin/env bash
# 此脚本用于播放动漫仙逆（网站：动漫巴士）
# 判断是哪个动漫
   case $1 in
        xn)
            mingchen="仙逆"
            donghua=4342;;
        tsxk)
            mingchen="吞噬星空"
            donghua=35;;
        lzr)
            mingchen="笼中人"
            donghua=5016;;
        jl)
            mingchen="剑来"
            donghua=5030;;
        wlq|ckwlq)
            mingchen="伍六七之记忆碎片"
            donghua=5098;;
        sxasx|sx)
            mingchen="师兄啊师兄"
            donghua=3510;;
        wmsj)
            mingchen="完美世界"
            donghua=217;;
        hzw|hhw)
            mingchen="海贼王"
            donghua=71;;
        msj)
            mingchen="牧神记"
            donghua=5191;;
        csj)
            mingchen="长生界"
            donghua=5191;;
        *)
            h h comic
        exit;;
    esac

# 播放函数，$1为动漫序数，$2为动漫集数
function bofang(){
    if [[ ! -f /tmp/tmp_$1_$2.html ]]; then
        wget https://dm84.tv/p/$1-1-$2.html -O /tmp/tmp_$1_$2.html 2>/dev/null
    fi
    url=`awk -F "iframe" '{print $2}' /tmp/tmp_$1_$2.html |awk -F "\"" '{print $2}'`
    firefox $url &
    echo "正在加载，请稍后……"
    unset url
   }


# 判断要播放的集数，并播放
if [[ -z $2 ]]; then
    if [[ ! -f /tmp/tmp_$1.html ]]; then
        wget https://dm84.tv/v/$donghua.html -O /tmp/tmp_$1.html 2>/dev/null
    fi
    #newhua=`sed -r "s/.*第([0-9]*)话<.span><em class=.hr.*/\1/g" /tmp/tmp_$1.html`
    #echo -e "\e[33m" $mingchen "\e[0m最新一话是第\e[33m" $newhua "\e[0m话，请问是否要播放？[y/n]"
    newhua=`sed -r "s/.*update_new..content=.([10-9]+).*/\1/g" /tmp/tmp_$1.html`
    newdate=`sed -r "s/.*update_date..content=.(.{6,12}) .*/\1/g" /tmp/tmp_$1.html`
    echo -e "\n\e[33m" $mingchen "\e[0m 第\e[33m" $newhua "\e[0m话\n\n 更新日期：\e[33m"$newdate "\e[0m,请问是否要播放？[y/n]"
    read -r input
    if [[ $input == "y" || $input == "" ]]; then
        bofang $donghua $newhua
    fi
    unset newhua input donghua mingchen newdate
else
    bofang $donghua $2
    unset donghua mingchen
fi
      

#rm /tmp/tmp_$1_$2.html$1.html

